$.get("platform/getCurrent", function (platform) {
    if (platform) {
        $("#code").val(platform.code).prop("disabled", true);
        $("#name").val(platform.name);
        $("#shortName").val(platform.shortName);
        $("#url").val(platform.url);
    }
});

/**
 * 初始化表单验证
 * 
 * @param {type} param
 */
$("#platformForm").validate({
    //错误类名和元素标签类型
    errorClass: "gg-error",
    validClass: "gg-success",
    errorElement: "label",
    //验证规则正则表达式
    rules: {
        code: {
            required: true,
            regx: /^[a-zA-Z]{4}$/
        },
        name: {
            required: true
        }
    },
    //验证结果文字提示
    messages: {
        code: {
            required: '必填',
            regx: '格式不正确'
        },
        name: {
            required: '必填'
        }
    },
    //验证结果处理
    highlight: function (element, errorClass, validClass) {
        $(element).closest('.form-group').addClass(errorClass).removeClass(validClass);
    },
    unhighlight: function (element, errorClass, validClass) {
        $(element).closest('.form-group').removeClass(errorClass).addClass(validClass);
    },
    //错误信息位置
    errorPlacement: function (error, element) {
        error.insertAfter(element);
    }
});

/**
 * 提交表单
 * 
 * @param {type} param
 */
$("#savePlatformBtn").click(function () {
    if ($("#platformForm").valid()) {
        var data = $("#platformForm").formToJson();
        $.post("platform/save", data, function (res) {
            if (res.success) {
                alert(res.message);
                location.href = "login";
            } else {
                alert(res.message);
            }
        }).fail(function () {
            alert("网络通信错误");
        });
    }
});